package com.socialbank.web.service.impl;

import java.util.List;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.socialbank.web.bean.Good;
import com.socialbank.web.bean.GoodsRecommend;
import com.socialbank.web.mapper.RecommendGoodsMapper;
import com.socialbank.web.service.RecommendGoodsService;
@Service
public class RecommendGoodsServiceImpl implements RecommendGoodsService{
	
	@Autowired
	private SqlSession sqlSession;
	
	@Override
	public Integer addRecommendGoods(GoodsRecommend goodsRecommend) {
		// TODO Auto-generated method stub
		
		RecommendGoodsMapper mapper = sqlSession.getMapper(RecommendGoodsMapper.class);
		int modifyGoodsRecommend = 0;
		if (goodsRecommend.getRecommend() == 1) {
			Long id = Long.valueOf(RandomStringUtils.randomNumeric(18));
			Long deptId = Long.valueOf(RandomStringUtils.randomNumeric(18));
			goodsRecommend.setDeptId(deptId);
			goodsRecommend.setId(id);
			
			GoodsRecommend findRecommendOfGoodsId = mapper.findRecommendOfGoodsId(goodsRecommend);
			if (findRecommendOfGoodsId==null) {
				
				mapper.addRecommendGoods(goodsRecommend);
			}
			modifyGoodsRecommend = mapper.modifyGoodsRecommend(goodsRecommend);
			return modifyGoodsRecommend;
		}else{
			modifyGoodsRecommend = mapper.modifyGoodsRecommend(goodsRecommend);
			return modifyGoodsRecommend;
		}
	}

	

	@Override
	public List<Good> findRecommendGoods() {
		// TODO Auto-generated method stub
		RecommendGoodsMapper mapper = sqlSession.getMapper(RecommendGoodsMapper.class);
		List<Good> goodsRecommendList = mapper.findRecommendGoods();
		return goodsRecommendList;
	}

}
